package YAPserver.behaviours;

import java.util.Random;

import YAPserver.collisionEngine.*;
import YAPserver.gameObjects.Line;
import YAPserver.gameObjects.Point;

/**
 * The ball will bounce dynamically, so according to where it hits the slider.
 * The more the ball hits the slider towards the edge, the more the ball's direction 
 * is parallel to the slider.
 * @author Daniel
 *
 */
public class BounceDynamic extends BounceAbstract{
	
	@Override
	protected double calcNewAngle(Ball ball, Wall wall){
		Random rand = new Random();
		
		Line objectLine = wall.getLine();
		double objectAngle = objectLine.getAngle();
		Point middlePoint = ball.getNextMiddlePoint();
		double distance1 = middlePoint.distance(wall.getLine().getStartPoint());
		double distance2 = middlePoint.distance(wall.getLine().getEndPoint());
		double newAngle;
		if(distance2 <= 2*ball.getRadius()/3. && distance2 < distance1){
			newAngle = objectAngle + Math.PI/100 - 0.01*rand.nextDouble();
		}else if(distance1 <= 2*ball.getRadius()/3.){
			newAngle = objectAngle + 99*Math.PI/100 + 0.01*rand.nextDouble();
		}else{
			newAngle = objectAngle + Math.atan(distance2/distance1)*2 + 0.01*rand.nextDouble();
		}
		if(newAngle>2*Math.PI){
			newAngle = newAngle - 2*Math.PI;
		}
		return newAngle;
	}

}
